package com.ahdy.mapper;

import com.ahdy.entity.EmergencyContact;
import org.apache.ibatis.annotations.*;

import java.util.List;

// 4. 紧急联系人Mapper
@Mapper
public interface EmergencyContactMapper {

    @Insert("INSERT INTO emergency_contacts(elderly_id, name, phone, relation, address, is_primary, " +
            "priority_order, notes, created_at, updated_at) VALUES(#{elderlyId}, #{name}, #{phone}, " +
            "#{relation}, #{address}, #{isPrimary}, #{priorityOrder}, #{notes}, #{createdAt}, #{updatedAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(EmergencyContact contact);

    @Update("UPDATE emergency_contacts SET name=#{name}, phone=#{phone}, relation=#{relation}, " +
            "address=#{address}, is_primary=#{isPrimary}, priority_order=#{priorityOrder}, " +
            "notes=#{notes}, updated_at=#{updatedAt} WHERE id=#{id}")
    int update(EmergencyContact contact);

    @Delete("DELETE FROM emergency_contacts WHERE id=#{id}")
    int deleteById(Integer id);

    @Select("SELECT * FROM emergency_contacts WHERE id=#{id}")
    EmergencyContact findById(Integer id);

    @Select("SELECT * FROM emergency_contacts WHERE elderly_id=#{elderlyId} ORDER BY priority_order ASC")
    List<EmergencyContact> findByElderlyId(Integer elderlyId);

    @Select("SELECT * FROM emergency_contacts WHERE elderly_id=#{elderlyId} AND is_primary=1")
    EmergencyContact findPrimaryByElderlyId(Integer elderlyId);

    @Delete("DELETE FROM emergency_contacts WHERE elderly_id=#{elderlyId}")
    int deleteByElderlyId(Integer elderlyId);

    // 获取指定优先级范围内的联系人
    @Select("SELECT * FROM emergency_contacts WHERE elderly_id=#{elderlyId} AND priority_order <= #{maxPriority} ORDER BY priority_order ASC")
    List<EmergencyContact> findByElderlyIdAndPriority(@Param("elderlyId") Integer elderlyId, @Param("maxPriority") Integer maxPriority);
}
